from datetime import datetime, date
import json
from app.models.db_setup import db


class Product(db.Model):
    __tablename__ = "Product"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    category = db.Column(db.String(255), nullable=False)
    name = db.Column(db.String(255), nullable=False)
    preview_image_url = db.Column(db.String(255), nullable=False)
    warehouse = db.Column(db.String(255), nullable=False)
    date = db.Column(db.Date, nullable=False)

    # 配件相关
    metalMaterial = db.Column(db.String(100))
    jadeSize = db.Column(db.DECIMAL(10, 2))
    jadeMaterial = db.Column(db.String(100))
    accessoryOthers = db.Column(db.Text)

    # 单选字段
    pearlMaterial = db.Column(db.String(100))
    pearlRoundness = db.Column(db.String(100))
    pearlClarity = db.Column(db.String(100))

    # 备注
    remark = db.Column(db.Text)

    # JSON字段
    channel = db.Column(db.JSON)
    skus = db.Column(db.JSON)

    # 其他字段
    created_by = db.Column(db.Integer, db.ForeignKey("User.id"))
    created_at = db.Column(db.DateTime, server_default=db.func.now())
    updated_at = db.Column(
        db.DateTime, server_default=db.func.now(), onupdate=db.func.now()
    )
    # 软删除标记
    deleted = db.Column(db.Boolean, default=False, nullable=False)

    # 关联关系
    creator = db.relationship("User", backref=db.backref("products", lazy=True))

    def __init__(
        self,
        category,
        name,
        preview_image_url,
        warehouse,
        date,
        metalMaterial=None,
        jadeSize=None,
        jadeMaterial=None,
        accessoryOthers=None,
        pearlMaterial=None,
        pearlRoundness=None,
        pearlClarity=None,
        remark=None,
        channel=None,
        skus=None,
        created_by=None,
    ):
        self.category = category
        self.name = name
        self.preview_image_url = preview_image_url
        self.warehouse = warehouse
        self.date = date
        self.metalMaterial = metalMaterial
        self.jadeSize = jadeSize
        self.jadeMaterial = jadeMaterial
        self.accessoryOthers = accessoryOthers
        self.pearlMaterial = pearlMaterial
        self.pearlRoundness = pearlRoundness
        self.pearlClarity = pearlClarity
        self.remark = remark
        self.channel = channel or []
        self.skus = skus or []
        self.created_by = created_by

    def to_dict(self):
        return {
            "id": self.id,
            "category": self.category,
            "name": self.name,
            "preview_image_url": self.preview_image_url,
            "warehouse": self.warehouse,
            "date": self.date.strftime("%Y-%m-%d" if self.date else None),
            "metalMaterial": self.metalMaterial,
            "jadeSize": float(self.jadeSize) if self.jadeSize is not None else None,
            "jadeMaterial": self.jadeMaterial,
            "accessoryOthers": self.accessoryOthers,
            "pearlMaterial": self.pearlMaterial,
            "pearlRoundness": self.pearlRoundness,
            "pearlClarity": self.pearlClarity,
            "remark": self.remark,
            "channel": self.channel,
            "skus": self.skus,
            "created_by": self.created_by,
            "created_at": self.created_at.isoformat() if self.created_at else None,
            "updated_at": self.updated_at.isoformat() if self.updated_at else None,
            "deleted": self.deleted,
        }


# 常量定义
PRODUCT_CATEGORIES = {
    "ES": "耳饰",
    "XL": "项链",
    "SL": "手链",
    "JZ": "戒指",
    "XZ": "胸针",
    "QT": "其他",
}

PEARL_COLORS = ["白色", "金色", "黑色", "灰色", "粉色", "其他"]
PEARL_ROUNDNESS = ["正圆", "圆", "椭圆", "异形", "其他"]
PEARL_CLARITY = ["无暇", "微瑕", "螺纹", "其他"]
PEARL_MATERIALS = ["海水珍珠", "淡水珍珠", "人造/贝珠", "其他", "无珍珠"]
METAL_MATERIALS = ["铜镀金", "银镀金", "925银", "K金", "足金", "其他"]
SALES_CHANNELS = ["微信", "抖音", "淘宝", "京东", "小红书", "其他"]
